// /*
//  * @Author: WanYiMing
//  * @Date:2024-07-08-11:10
//  * @Description:
//  */

using System;
using System.Collections.Generic;
using Code.GameLogic.XianJian.Controller;
using UnityEngine;

namespace Code.GameLogic.XianJian.XianJianBehavior
{
    public class XianJianBehaviorController : MonoBehaviour
    {
        private List<AbsXianJianBehavior> _XianJianBehaviorList;

        public List<GameObject> seeObjectList;

        public XianJianController XianJianController;

        public AbsXianJianBehavior Behavioring;
        

        private AbsXianJianBehavior InitXianJianBehaviorForCode(string XianJianBehavior)
        {
            Type type = Type.GetType(XianJianBehavior);
        
            if (type != null && typeof(AbsXianJianBehavior).IsAssignableFrom(type))
            {
                object instance = Activator.CreateInstance(type);
                var behavior = (AbsXianJianBehavior)instance;
                return behavior;
            }
            return null;
        }
        
        
        public void LoadBehavior(List<string> XianJianBehaviorList, XianJianController tXianJianController)
        {
            XianJianController = tXianJianController;
            seeObjectList = new List<GameObject>();
            _XianJianBehaviorList = new List<AbsXianJianBehavior>();
            XianJianBehaviorList.ForEach(XianJianBehaviorCode=>_XianJianBehaviorList.Add(InitXianJianBehaviorForCode(XianJianBehaviorCode)) );
            _XianJianBehaviorList.ForEach(XianJianBehavior => XianJianBehavior.InitAbsBehavior(XianJianController));
        }

        public void InView(GameObject tigerObject)
        {
            seeObjectList.Add(tigerObject);
            TigerBehavior(tigerObject,ObjectBehaviorEnum.InView);
        }
        
        public void OutView(GameObject tigerObject)
        {
            seeObjectList.Remove(tigerObject);
            TigerBehavior(tigerObject,ObjectBehaviorEnum.OutView);
        }

        
        private void TigerBehavior(GameObject tigerGameObject,ObjectBehaviorEnum objectBehavior)
        {
            _XianJianBehaviorList.ForEach(XianJianBehavior => XianJianBehavior.TigerBehavior(tigerGameObject,objectBehavior));
        }

        
        private void AutoBehavior()
        {
            _XianJianBehaviorList.ForEach(XianJianBehavior =>
            {
                if (XianJianBehavior.IsBehavior())
                {
                    XianJianBehavior.AutoBehavior();
                }
            });
        }
        
        private void Update()
        {
            AutoBehavior();
        }
       
    }
    
    public enum ObjectBehaviorEnum
    {
        InView,
        OutView,
        Death,            
    }
    
}